package com.orange.controller.client;

import cn.hutool.core.util.ObjectUtil;
import com.orange.common.annotation.RequestPage;
import com.orange.common.annotation.NotRepeatSubmit;
import com.orange.common.annotation.PassToken;
import com.orange.common.base.controller.impl.BaseControllerImpl;
import com.orange.common.page.PageParam;
import com.orange.common.page.PageQuery;
import com.orange.common.page.PageResult;
import com.orange.common.utils.IpUtil;
import com.orange.framework.result.R;
import com.orange.project.client.pojo.ClientReq;
import com.orange.project.client.pojo.ClientRes;
import com.orange.project.client.pojo.ClientShareReq;
import com.orange.project.client.pojo.ClientShareRes;
import com.orange.project.client.service.ClientService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * TODO
 *
 * @Author YWF
 * @Date 2025/8/14 9:45
 */
@Validated
@RestController
@RequestMapping("/client/")
public class ClientController extends BaseControllerImpl {

    protected final static Logger logger = LoggerFactory.getLogger(ClientController.class);

    @Autowired
    private ClientService clientService;

    @NotRepeatSubmit
    @PassToken
    @PostMapping(value = "loginClient")
    public R loginClient(@RequestBody ClientReq clientReq) {
        String ipAddress = IpUtil.getIpAddr(request);
        clientReq.setRemoteIP(ipAddress);
        ClientRes clientRes = clientService.saveClientInfo(clientReq);
        if (ObjectUtil.isNotNull(clientRes)) {
            clientService.saveClientLog(clientReq);
            return R.ok().data(clientRes);
        }
        return R.error().message("客户端登录失败");
    }

    @NotRepeatSubmit
    @PassToken
    @PostMapping(value = "saveClientShare")
    public R saveClientShare(@RequestBody ClientShareReq clientShareReq) {
        clientService.saveClientShare(clientShareReq);
        return R.ok();
    }

    @PassToken
    @GetMapping(value = "getShareList")
    public R<PageResult<ClientShareRes>> getShareList(@RequestBody @Valid PageQuery<ClientShareReq> pageQuery) {
        com.orange.common.page.PageParam pageParam = pageQuery.getPageParam();
        ClientShareReq query = pageQuery.getQuery();
        PageResult<ClientShareRes> shareResList = clientService.getShareList(query, pageParam);
        return R.ok().data(shareResList);
    }

    /**
     * 分离参数接收方式
     * {
     *   "pagination": {
     *     "page": 1,
     *     "size": 10,
     *     "sort": [
     *       {
     *         "field": "create_time",
     *         "direction": "DESC"
     *       }
     *     ]
     *   },
     *   "query": {
     *     "uids": "XLMV5",
     *     "langId": "测试123123",
     *     "content": "as:asew:rew"
     *   }
     * }
     */
    @PassToken
    @PostMapping(value = "getList")
    public R getUsers(@Validated @RequestBody PageQuery<ClientShareReq> request) {
        ClientShareReq query = request.getQuery();
        PageParam pagination = request.getPageParam();
        PageResult<ClientShareRes> shareResList = clientService.findList(query, pagination);
        return R.ok().data(shareResList);
    }

    /**
     * 分离参数接收方式
     * 可以正确获取 page=1&size=10&sort=name,asc&id=1001&name=张三 这些值
     */
    @PassToken
    @GetMapping(value = "getList1")
    public R getUsers1(@RequestPage PageParam pagination,
                       @Validated @ModelAttribute ClientShareReq query) {
        PageResult<ClientShareRes> shareResList = clientService.findList(query, pagination);
        return R.ok().data(shareResList);
    }

}

